<?php
namespace app\admin\controller\user;
use app\admin\model\openservice\Openapps;
use app\admin\model\user\User;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use think\Request;

/**
 * @NodeAnnotation(
 *     title="第三方授权用户",
 *     parent="admin/user.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1
 * )
 */
class AdminOauth extends Admin
{
    use ControllerCurd;

    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\user\AdminOauth();
    }

    /**
     * @NodeAnnotation(
     *     title="第三方用户",
     *     parent="admin/user.admin_oauth",
     *     is_menu=1,
     *     is_view=1,
     *     auth=1,
     *     MenuRoot="admin/user.index"
     * )
     */
    public function index(Request $request)
    {
        $where    = [];
        $keywords = $request->param("keywords");

        if($keywords)
        {
            array_push($where,["a.nickname",'eq',$keywords]);
        }


        $userList  = $this->model
            ->field('a.*,u.nickname,u.sex,u.avatar,u.username,u.phone')
            ->alias('a')
            ->leftJoin('cp_admin u', 'a.user_id = u.id')
            ->where($where)
            ->where("a.status", 1)
            ->order("create_time DESC")
            ->paginate();
        $this->assign([
            "userList" => $userList,
            "search"   => [
                "keywords" => $keywords
            ]
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="创建第三方用户",
     *     parent="admin/user.admin_oauth",
     *     MenuRoot="admin/user.index",
     *     is_menu=0
     * )
     */
    public function adduser(Request $request)
    {
        $this->add(url("admin/user.admin_oauth/index"));
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑第三方用户",
     *     parent="admin/user.admin_oauth",
     *     MenuRoot="admin/user.index",
     *     is_menu=0
     * )
     */
    public function edituser(Request $request,$id=0)
    {
        $this->edit(url("admin/user.admin_oauth/index"));

        $user = $this->model
            ->alias("a")
            ->leftJoin ("cp_admin b","a.uid=b.id")
            ->field("a.id,a.uid,b.username,a.status")
            ->group("a.uid")
            ->where("a.id",$id)
            ->find();

        $this->assign([
            "user" => $user
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除第三方用户",
     *     parent="admin/user.admin_oauth",
     *     MenuRoot="admin/user.index",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function deluser(Request $request)
    {
        $this->del();
    }

    /**
     * @NodeAnnotation(
     *     title="导出数excel",
     *     parent="admin/user.admin_oauth",
     *     MenuRoot="admin/user.index",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function excel()
    {
        $user = $this->model->select()->toArray();
        $data = [];
        foreach ($user as $item)
        {
            $item['telphone'] = isset($item['more']['telphone'])?$item['more']['telphone']:$item['nickname'];
            array_push($data,$item);
        }
        $header = [
            "telphone"    => "电话",
            "create_time" => "注册时间",
            "phone"       => "电话",
        ];
        importExcel($data,$header,time());
    }
}